package com.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.controller.utils.R;
import com.springboot.dao.BookDao;
import com.springboot.domain.Book;
import com.springboot.service.BookService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public IPage getAll(Integer currentPage, Integer pageSize, Book book) {
        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Book> lqw=new LambdaQueryWrapper<>();
        //拼接sql,Book::getType得到数据库字段名，book.getType()是传过来的type的值
        lqw.like(Strings.isNotEmpty(book.getType()),Book::getType,book.getType());
        lqw.like(Strings.isNotEmpty(book.getName()),Book::getName,book.getName());
        lqw.like(Strings.isNotEmpty(book.getDescription()),Book::getDescription,book.getDescription());
        return bookDao.selectPage(page,lqw);
    }

    @Override
    public void insert(Book book) {
        bookDao.insert(book);
    }

    @Override
    public void updateById(Book book) {
        bookDao.updateById(book);
    }

    @Override
    public Book getById(Integer id) {
        return bookDao.selectById(id);
    }

    @Override
    public void deleteById(Integer id) {
        bookDao.deleteById(id);
    }

}
